﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Shell32; 
using System.IO;
using System.Collections;
using Aspose.Cells;

namespace Expressing_Label
{
    public partial class test : Form
    {
        private string ts = "";
        public test()
        {
            InitializeComponent();
            
        }

        private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)
        {

            GraphicsPath graphicsPath = new GraphicsPath();


            Color colorTransparent = bitmap.GetPixel(0, 0);

            int colOpaquePixel = 0;

            for (int row = 0; row < bitmap.Height; row++)
            {

                colOpaquePixel = 0;

                for (int col = 0; col < bitmap.Width; col++)
                {

                    if (bitmap.GetPixel(col, row) != colorTransparent)
                    {

                        colOpaquePixel = col;

                        int colNext = col;

                        for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)
                            if (bitmap.GetPixel(colNext, row) == colorTransparent)
                                break;

                        graphicsPath.AddRectangle(new Rectangle(colOpaquePixel,
                         row, colNext - colOpaquePixel, 1));

                        col = colNext;
                    }
                }
            }

            return graphicsPath;
        }

        private void button1_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void button7_Paint(object sender, PaintEventArgs e)
        {
            Button b = (Button)sender;
            b.Size = b.BackgroundImage.Size;
            Bitmap bmpBob = (Bitmap)b.BackgroundImage;
            GraphicsPath graphicsPath = CalculateControlGraphicsPath(bmpBob);
            b.Region = new Region(graphicsPath);
        }

        Hashtable htf = new Hashtable();
        int localCount = 0;
        private void button7_Click(object sender, EventArgs e)
        {
            //string strFileBase = "I:\\Compare";
            //htf.Clear();
            //FindAllFile(new DirectoryInfo(strFileBase));
            //MessageBox.Show(localCount.ToString());
            //Hashtable hts = new Hashtable();

            //var db = new db_FACEEntities();
            //var result = from info in db.tb_ExpressingInfo
            //             select new
            //             {
            //                 ID = info.ID,
            //                 FileLocation = info.ImageLocation.Substring(8)
            //             };

            //foreach (var re in result)
            //{
            //    hts.Add(re.FileLocation, re.ID.ToString());
            //}

            //localCount = 0;
            //foreach (var f in htf.Keys)
            //{
            //    if (!hts.Contains(f))
            //    {
            //        string tts = htf[f].ToString();
            //        File.Delete(tts);
            //        tts = tts.Insert(tts.Length - 4, "_min");
            //        File.Delete(tts);
            //        localCount += 2;
            //    }
            //}
            //MessageBox.Show(localCount.ToString());
            
            //localCount = 0;
            //foreach (var f in hts.Keys)
            //{
            //    if (!htf.Contains(f))
            //    {
            //        var tb = new tb_ExpressingInfo { ID = Convert.ToInt32(hts[f]) };
            //        db.tb_ExpressingInfo.Attach(tb);
            //        db.tb_ExpressingInfo.Remove(tb);
            //        localCount++;
            //    }
            //}
            //db.SaveChanges();
            //MessageBox.Show(localCount.ToString());
        }

        private void FindAllFile(DirectoryInfo di)
        {
            foreach (FileInfo fi in di.GetFiles())
            {
                string ts = fi.ToString();
                if ((ts.EndsWith(".jpg") || ts.EndsWith(".png")) && !ts.Contains("min"))
                { 
                    //lstFile.Add(ts.Substring(0, ts.Length - 4));
                    //delete from tb_ExpressingInfo where ImageLocation in (select ImageLocation from tb_ExpressingInfo group by ImageLocation  having count(ImageLocation) > 1) and ID not in (select min(ID) from tb_ExpressingInfo group by ImageLocation having count(ImageLocation )>1)

                    string tts = ts.Substring(0, ts.Length - 4);
                    if (htf.Contains(tts))
                    {
                        tts = fi.FullName;
                        File.Delete(tts);                       
                        tts = tts.Insert(tts.Length - 4, "_min");
                        File.Delete(tts);
                        localCount += 2;
                    }
                    else
                        htf.Add(ts.Substring(0, ts.Length - 4), fi.FullName);
                }
            }
            foreach (DirectoryInfo cdi in di.GetDirectories())
            {
                FindAllFile(cdi);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new PublicResources();
            Workbook wb = new Workbook();
            Worksheet ws = wb.Worksheets[0];
            Aspose.Cells.Style style = wb.Styles[wb.Styles.Add()];
            int colIndex = 0;
            ws.Cells[0, colIndex++].Value =  "被试序号";
            ws.Cells[0, colIndex++].Value = "神经质";
            ws.Cells[0, colIndex++].Value = "外倾性";
            ws.Cells[0, colIndex++].Value = "开放性";
            ws.Cells[0, colIndex++].Value = "宜人性";
            ws.Cells[0, colIndex++].Value = "责任心";            
            ws.Cells[0, colIndex++].Value = "积极";
            ws.Cells[0, colIndex++].Value = "消极";

            ws.Cells[0, colIndex++].Value = "高兴";
            ws.Cells[0, colIndex++].Value = "强度";
            ws.Cells[0, colIndex++].Value = "惊奇";
            ws.Cells[0, colIndex++].Value = "强度";
            ws.Cells[0, colIndex++].Value = "专注";
            ws.Cells[0, colIndex++].Value = "强度";


            ws.Cells[0, colIndex++].Value = "疲倦";
            ws.Cells[0, colIndex++].Value = "强度";
            ws.Cells[0, colIndex++].Value = "走神";
            ws.Cells[0, colIndex++].Value = "强度";
            ws.Cells[0, colIndex++].Value = "厌烦";
            ws.Cells[0, colIndex++].Value = "强度";

            ws.Cells[0, colIndex++].Value = "困惑";
            ws.Cells[0, colIndex++].Value = "强度";
            ws.Cells[0, colIndex++].Value = "挫败";
            ws.Cells[0, colIndex++].Value = "强度";

            ws.Cells[0, colIndex++].Value = "积极";
            ws.Cells[0, colIndex++].Value = "消极";
            var db = new db_FACEEntities();
            var result = from info in db.tb_UserInfo
                         where info.UserID >= 50
                         select info;
            int rowIndex = 1;
            string[] ts;
            foreach (var re in result)
            {
                colIndex = 0;
                ws.Cells[rowIndex, colIndex++].Value = re.UserID;
                ts = re.CharecterResult.Split(',');
                for (int i = 0; i < 5; i++)
                {
                    ws.Cells[rowIndex, colIndex + i].Value = ts[i];
                }
                colIndex += 5;
                ts = re.FeelingResult.Split(',');
                ws.Cells[rowIndex, colIndex++].Value = ts[0];
                ws.Cells[rowIndex, colIndex++].Value = ts[1];
                FileStream fs = new FileStream(@"CaptureAnalysis\" + re.UserID.ToString() + ".data", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string tsr = string.Empty;
                for (int j = 0; j < 15; j++)
                {
                    tsr = sr.ReadLine();
                    ts = tsr.Split(' ');
                    for (int k = 0; k < 25; k++)
                    {
                        PublicResources.g_ExpresingCaptureRecord[j, k] = Convert.ToInt32(ts[k]);
                    }
                }
                int t = PublicResources.g_ExpressingSelectedIndex["高兴"] + 5;
                ws.Cells[rowIndex, colIndex++].Value = PublicResources.g_ExpresingCaptureRecord[12, t];
                ws.Cells[rowIndex, colIndex++].Value = "暂未记录";
                t = PublicResources.g_ExpressingSelectedIndex["惊奇"] + 5;
                ws.Cells[rowIndex, colIndex++].Value = PublicResources.g_ExpresingCaptureRecord[12, t];
                ws.Cells[rowIndex, colIndex++].Value = "暂未记录";
                t = PublicResources.g_ExpressingSelectedIndex["专注"] + 5;
                ws.Cells[rowIndex, colIndex++].Value = PublicResources.g_ExpresingCaptureRecord[12, t];
                ws.Cells[rowIndex, colIndex++].Value = "暂未记录";

                t = PublicResources.g_ExpressingSelectedIndex["疲倦"] + 5;
                ws.Cells[rowIndex, colIndex++].Value = PublicResources.g_ExpresingCaptureRecord[12, t];
                ws.Cells[rowIndex, colIndex++].Value = "暂未记录";
                t = PublicResources.g_ExpressingSelectedIndex["走神"] + 5;
                ws.Cells[rowIndex, colIndex++].Value = PublicResources.g_ExpresingCaptureRecord[12, t];
                ws.Cells[rowIndex, colIndex++].Value = "暂未记录";
                t = PublicResources.g_ExpressingSelectedIndex["厌烦"] + 5;
                ws.Cells[rowIndex, colIndex++].Value = "暂未记录";

                ws.Cells[rowIndex, colIndex++].Value = PublicResources.g_ExpresingCaptureRecord[12, t];
                t = PublicResources.g_ExpressingSelectedIndex["困惑"] + 5;
                ws.Cells[rowIndex, colIndex++].Value = PublicResources.g_ExpresingCaptureRecord[12, t];
                ws.Cells[rowIndex, colIndex++].Value = "暂未记录";
                t = PublicResources.g_ExpressingSelectedIndex["挫败"] + 5;
                ws.Cells[rowIndex, colIndex++].Value = PublicResources.g_ExpresingCaptureRecord[12, t];
                ws.Cells[rowIndex, colIndex++].Value = "暂未记录";

                ws.Cells[rowIndex, colIndex++].Value = PublicResources.g_ExpresingCaptureRecord[12, 1];
                ws.Cells[rowIndex++, colIndex++].Value = PublicResources.g_ExpresingCaptureRecord[12, 2] + PublicResources.g_ExpresingCaptureRecord[12, 3];
                
            }
            wb.Save(@"Data\ok.xls");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //new PublicResources();
            //var db = new db_FACEEntities();
            //tb_UserInfo tb = new tb_UserInfo();
            //tb.UserID = PublicResources.g_iUserID;
            //tb.Gender = PublicResources.g_blUserGender;
            //tb.Department = PublicResources.g_strUserDepartment;
            //tb.Character = PublicResources.g_strUserCharacter;
            //tb.SelfDiscribing = PublicResources.g_strSelfDiscribing;
            //tb.PHysicalCondition = PublicResources.g_strPhysicalCondition;
            //tb.SumOfExpressing = PublicResources.g_ExpresingCaptureRecord[12, 0];
            //tb.SumOfDetail = PublicResources.g_ExpresingCaptureRecord[12, 1].ToString() + "," + PublicResources.g_ExpresingCaptureRecord[12, 2].ToString() + "," + PublicResources.g_ExpresingCaptureRecord[12, 3].ToString() + "," + PublicResources.g_ExpresingCaptureRecord[12, 4].ToString();
            //tb.VideoEffectivity = PublicResources.g_strVideoEffectivity;
            //tb.CharacterQuery = PublicResources.g_strCharacter; ;
            //tb.FeelingShowing = PublicResources.g_strFeeling;
            //tb.CharecterResult = PublicResources.g_strCharacterResult;
            //tb.FeelingResult = PublicResources.g_strFeelingResult;
            //tb.ExperimentID = PublicResources.g_iExperimentID;
            //var result = from info in db.tb_UserInfo
            //             where info.UserID == PublicResources.g_iUserID
            //             select new
            //             {
            //                 id = info.ID
            //             };
            //if (result.Count() == 0)
            //    db.tb_UserInfo.Add(tb);
            //else
            //{
            //    var re = result.ToArray();
            //    tb.ID = re[0].id;
            //    db.tb_UserInfo.Attach(tb);
            //    db.Entry(tb).State = System.Data.Entity.EntityState.Modified;
            //}
            //db.SaveChanges();
        }
    }
}
